package com.kerrykidz.scminvoicing.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.PrimaryKeyGenerator.BusinessType;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.scminvoicing.dao.model.ScmPayformEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPayformOrderEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPurOrderEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmSupEntity;
import com.kerrykidz.scminvoicing.service.IScmPayformService;
import com.kerrykidz.scminvoicing.service.IScmPurOrderService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.util.ConstantUtil;
import com.kerrykidz.system.util.DataSourceUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.ExportParamVo;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.kerrykidz.workflow.vo.OptHisVo;
import com.kerrykidz.workflow.vo.WorkflowParamVo;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.lang.StringUtil;

@Controller
@RequestMapping("scmInvocing/scmPayForm")
public class ScmPayformController {
	@Autowired
	private IScmPayformService iScmPayformService;
	@Autowired
	DataSourceUtil das;
	@Autowired
	private IScmPurOrderService iScmPurOrderService;
	@Autowired
	IWorkflowService workflowService;

	@Autowired
	private ICommonService commonService;

	/**
	 * 付款申请单列表查询
	 * 
	 * @Description ModelAndView
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/findInvoOrderPay")
	public ModelAndView findInvoOrderPay() throws RDPException {
		ModelAndView mav = new ModelAndView("invoicing/invo_orderPay");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				mav.addObject("branList", branList);
				mav.addObject("isZongBU", 1);
			} else {
				branList = commonService.getBranListForStock(brandId);
				mav.addObject("branList", branList);
				mav.addObject("isZongBU", 0);
			}
		}
		return mav;
	}

	/**
	 * 付款申请单列表查询
	 * 
	 * @Description ScmPurOrderEntity
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/findScmPayformEntityList")
	@ResponseBody
	public ScmPayformEntity findScmPayformEntityList(ScmPayformEntity scmPayform)
			throws RDPException {
		scmPayform.setIspaging(true);
		String startTime = scmPayform.getStartApplyTime();
		if (null != startTime && !"".equals(startTime)) {
			scmPayform.setStartApplyTime(startTime + " 00:00:00");
		}
		String endTime = scmPayform.getEndApplyTime();
		if (null != endTime && !"".equals(endTime)) {
			scmPayform.setEndApplyTime(endTime + " 23:59:59");
		}
		String userId = UserUtil.getUserFromSession().getUserId();
		 scmPayform.setUserId(userId);
		List<ScmPayformEntity> scmPayformList = iScmPayformService
				.findScmPayformEntityList(scmPayform);
		if (null != scmPayformList && scmPayformList.size() > 0) {
			for (ScmPayformEntity p : scmPayformList) {
				p.setWorkflowVo(workflowService.getWorkflowVo(
						p.getFkFlowinstId(), p.getStatus(),
						UserUtil.getUserFromSession()));
			}
		}
		scmPayform.setResults(scmPayformList);
		return scmPayform;
	}

	/**
	 * 新增付款申请 时 的 付款申请单列表
	 * 
	 * @Description ScmPurOrderEntity
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/findScmPurOrderPayForm")
	@ResponseBody
	public ScmPurOrderEntity findScmPurOrderPayForm(
			ScmPurOrderEntity scmPurOrder) throws RDPException {
		scmPurOrder.setIspaging(true);
		UserInfo user = UserUtil.getUserFromSession();
		scmPurOrder.setFkBranId(user.getBranId());
		String startTime = scmPurOrder.getStartApplyTime();
		if (null != startTime && !"".equals(startTime)) {
			scmPurOrder.setStartApplyTime(startTime + " 00:00:00");
		}
		String endTime = scmPurOrder.getEndApplyTime();
		if (null != endTime && !"".equals(endTime)) {
			scmPurOrder.setEndApplyTime(endTime + " 23:59:59");
		}
		if (null == scmPurOrder.getSelectId() || scmPurOrder.getSelectId().size() == 0) {
			scmPurOrder.setSelectId(null);
		}
		List<ScmPurOrderEntity> scmPurOrderList = iScmPayformService
				.findScmPurOrderPayForm(scmPurOrder);
		scmPurOrder.setResults(scmPurOrderList);
		return scmPurOrder;
	}

	/**
	 * 新增付款申请 时 的 付款申请单列表
	 * 
	 * @Description ScmPurOrderEntity
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/scmPurOrderListByPayFormId")
	@ResponseBody
	public Map<String, Object> scmPurOrderListByPayFormId(HttpSession session,
			@RequestParam("pkPayFormId") String pkPayFormId)
			throws RDPException {
		List<ScmPurOrderEntity> scmPurOrderList = iScmPayformService
				.scmPurOrderListByPayFormId(pkPayFormId);
		Map<String, Object> mapScmPurOrder = new HashMap<String, Object>();
		mapScmPurOrder.put("scmPurOrderList", scmPurOrderList);
		return mapScmPurOrder;
	}

	/**
	 * 新增付款申请 时 的 付款申请单列表
	 * 
	 * @Description ScmPurOrderEntity
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/scmPurOrderListSupIdAndByPayFormId")
	@ResponseBody
	public Map<String, Object> scmPurOrderListSupIdAndByPayFormId(@RequestParam("supplyId") String supplyId,
			@RequestParam("pkPayFormId") String pkPayFormId)
			throws RDPException {
		List<ScmPurOrderEntity> scmPurOrderList = iScmPayformService
				.scmPurOrderListSupIdAndByPayFormId(supplyId,pkPayFormId);
		Map<String, Object> mapScmPurOrder = new HashMap<String, Object>();
		mapScmPurOrder.put("scmPurOrderList", scmPurOrderList);
		return mapScmPurOrder;
	}

	
	/**
	 * 字符串伪数组 去重
	 * 
	 * @Description String
	 * @author chenyu 2014年9月9日 下午7:57:09
	 */
	public static String getStrsToUniqeString(String Strs) {

		String strsNew = Strs;
		if (Strs.indexOf(",") != -1) {
			String[] str = Strs.split(",");
			for (int i = 0; i < str.length; i++) {
				if (i == 0) {
					strsNew += str[i];
				} else {
					strsNew += "," + str[i];
				}
			}
		}
		return strsNew;
	}

	/**
	 * 跳转到 新增款申请单清单页面
	 * 
	 * @Description ModelAndView
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/invoOrderPayNew")
	@ResponseBody
	public ModelAndView invoOrderPayNew(@RequestParam("type") String type,
			@RequestParam("pkPayFormId") String pkPayFormId)
			throws RDPException {
		String modelView = "invoicing/invo_orderPay_new";
		if (type.equals("detail")) {
			modelView = "invoicing/invo_orderPaydetail";
		} else if (type.equals("chuli")) {
			modelView = "invoicing/invo_orderPay_new_s";
		} else if (type.equals("chuli-tui")) {
			modelView = "invoicing/invo_orderPay_Tuinew_s";
		}
		String oldSuppLyId = "";
		String supplyName = "";
		ScmPayformEntity scmPayform = new ScmPayformEntity();
		List<OptHisVo> hisWorkFlowList = null;
		ModelAndView mav = new ModelAndView(modelView);
		UserInfo user = UserUtil.getUserFromSession();
		String fkBranId = user.getBranId();
		String abbrCode = user.getAbbrCode();
		if(abbrCode.toUpperCase().indexOf("ZB") != -1)
		{
			fkBranId = null;
		}
		List<ScmSupEntity> scmSupList = iScmPurOrderService
				.PurOrderSupplyList(fkBranId);
		if (StringUtils.isBlank(pkPayFormId)) {
			pkPayFormId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.FK,
					user.getAbbrCode());
		} else {
			scmPayform = iScmPayformService.getScmPayformById(pkPayFormId);
			if (null != scmPayform) {
				if (StringUtils.isNotBlank(scmPayform.getFkFlowinstId())) {
					/** 获取审批流程历史 */
					hisWorkFlowList = workflowService.findOpinions(
							scmPayform.getFkFlowinstId(), user);
				}
				oldSuppLyId = scmPayform.getFkSupId();
				for (ScmSupEntity scmSupply : scmSupList) {
					if (oldSuppLyId.equals(scmSupply.getPkSupId())) {
						supplyName = scmSupply.getSupName();
						break;
					}
				}
			}
		}
		mav.addObject("type", type);
		mav.addObject("hisWorkFlowList", hisWorkFlowList);
		mav.addObject("scmPayform", scmPayform);
		mav.addObject("oldSuppLyId", oldSuppLyId);
		mav.addObject("supplyName", supplyName);
		mav.addObject("userName", user.getName());
		mav.addObject("scmSupList", scmSupList);
		mav.addObject("brandName", user.getBranName());
		mav.addObject("pkPayFormId", pkPayFormId);
		return mav;
	}

	/**
	 * add付款申请单
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/addScmPayform")
	@ResponseBody
	public String addScmPayform(ScmPayformEntity scmPayform)
			throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		String pkPayformId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.FK,
				user.getAbbrCode());
		String abbrCode = user.getBranId();
		String userId = user.getUserId();
		scmPayform.setApplyBy(userId);
		scmPayform.setModiBy(userId);
		scmPayform.setFkBranId(abbrCode);
		scmPayform.setPkPayformId(pkPayformId);
		scmPayform.setDelFlag("0");
		iScmPayformService.addScmPayform(scmPayform);
		return "1";
	}

	/**
	 * update付款申请单
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/updateScmPayform")
	@ResponseBody
	public String updateScmPayform(ScmPayformEntity scmPayform)
			throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		String pkPayformId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.FK,
				user.getAbbrCode());
		String abbrCode = user.getBranId();
		String userId = user.getUserId();
		scmPayform.setApplyBy(userId);
		scmPayform.setModiBy(userId);
		scmPayform.setFkBranId(abbrCode);
		scmPayform.setPkPayformId(pkPayformId);
		scmPayform.setModiTime(new Date());
		iScmPayformService.updateScmPayform(scmPayform);
		return "1";
	}

	/**
	 * 单选时 add 付款申请单明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/insertPayFormDetail")
	@ResponseBody
	public String insertPayFormDetail(
			@RequestParam("pkPayFormId") String pkPayFormId,
			@RequestParam("pkPurordId") String pkPurordId,
			@RequestParam("type") String type,
			@RequestParam("scmSupIds") String scmSupIds) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		ScmPayformEntity scmPayform = null;
		ScmPayformOrderEntity scmPayformOrder = null;
		scmPayform = iScmPayformService.getScmPayformById(pkPayFormId);
		String resultMsg = "1";
		if (null == scmPayform) {
			scmPayform = new ScmPayformEntity();
			String userId = user.getUserId();
			String abbrCode = user.getBranId();
			scmPayform.setApplyBy(userId);
			scmPayform.setModiBy(userId);
			scmPayform.setFkBranId(abbrCode);
			scmPayform.setStatus("1");// 1 草稿状态
			scmPayform.setFkSupId(scmSupIds);
			scmPayform.setPkPayformId(pkPayFormId);
			scmPayform.setDelFlag("1");// 初始为删除状态，提交或保存后变成“0"
			iScmPayformService.addScmPayform(scmPayform);
		} else {
			if (type.equals("cancel")) {
				iScmPayformService.delScmPayformOrderByTwoId(pkPayFormId,
						pkPurordId);
			} else {
				scmPayformOrder = iScmPayformService.getPayFormOrderByTwoId(
						pkPayFormId, pkPurordId);
			}
		}
		if (null == scmPayformOrder && !type.equals("cancel")) {
			scmPayformOrder = new ScmPayformOrderEntity();
			scmPayformOrder.setFkPayformId(pkPayFormId);
			scmPayformOrder.setFkPurordId(pkPurordId);
			iScmPayformService.addScmPayFormOrder(scmPayformOrder);
		}

		return resultMsg;
	}

	/**
	 * 全选时 批量 add付款申请单,付款申请单商品明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/addPayFormOrderPrdBatch")
	@ResponseBody
	public String addPayFormOrderPrdBatch(
			@RequestParam("pkPayFormId") String pkPayFormId,
			@RequestParam("fkPurOrders") String fkPurOrders,
			@RequestParam("type") String type,
			@RequestParam("scmSupIds") String scmSupIds) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		ScmPayformEntity scmPayform = null;
		ScmPayformOrderEntity scmPayformOrder = null;

		scmPayform = iScmPayformService.getScmPayformById(pkPayFormId);
		String[] fkPurOrderArray = fkPurOrders.split(",");
		List<ScmPayformOrderEntity> scmPayFormOrderList = new ArrayList<ScmPayformOrderEntity>();
		if (null == scmPayform) {// 付款申请单 数据 未生成 时，先生成 付款申请单，再批量 生成初始全选的 订单商品明细。
			scmPayform = new ScmPayformEntity();
			String userId = user.getUserId();
			String abbrCode = user.getBranId();
			scmPayform.setApplyBy(userId);
			scmPayform.setModiBy(userId);
			scmPayform.setFkBranId(abbrCode);
			scmPayform.setStatus("1");// 1 草稿状态
			scmPayform.setFkSupId(scmSupIds);
			scmPayform.setPkPayformId(pkPayFormId);
			scmPayform.setDelFlag("1");
			iScmPayformService.addScmPayform(scmPayform);

			for (String fkPurordId : fkPurOrderArray) {
				scmPayformOrder = new ScmPayformOrderEntity();
				scmPayformOrder.setFkPayformId(pkPayFormId);
				scmPayformOrder.setFkPurordId(fkPurordId);
				scmPayFormOrderList.add(scmPayformOrder);
			}
			iScmPayformService.addScmPayFormOrderBatch(scmPayFormOrderList);
		} else {// 付款申请单 数据 已经生成过 时，for循环 先检测 已经生成的 商品明细，再 批量 生成的 订单商品明细。
			for (String fkPurordId : fkPurOrderArray) {
				if (type.equals("cancel")) {
					iScmPayformService.delScmPayformOrderByTwoId(pkPayFormId,
							fkPurordId);
				} else {
					scmPayformOrder = iScmPayformService
							.getPayFormOrderByTwoId(pkPayFormId, fkPurordId);
					if (null == scmPayformOrder) {// 未插入的商品明细，再 批量 生成订单商品明细。
						scmPayformOrder = new ScmPayformOrderEntity();
						scmPayformOrder.setFkPayformId(pkPayFormId);
						scmPayformOrder.setFkPurordId(fkPurordId);
						scmPayFormOrderList.add(scmPayformOrder);
					}
				}
			}
			if (null != scmPayFormOrderList && scmPayFormOrderList.size() > 0
					&& !type.equals("cancel")) {
				iScmPayformService.addScmPayFormOrderBatch(scmPayFormOrderList);
			}
		}

		return "1";
	}

	/**
	 * 点击 保存 按钮 ，save付款申请单,付款申请单明细
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/savePurOrderPrdBatch")
	@ResponseBody
	public String savePurOrderPrdBatch(
			@RequestParam("pkPayFormId") String pkPayFormId,
			@RequestParam("saveType") String saveType,
			@RequestParam("purStatus") String purStatus,
			@RequestParam("supplyId") String supplyId,ScmPayformEntity scmPayformParam) throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		//String fkBranId = user.getBranId();
		String userId = user.getUserId();
		//Date dateNow = new Date();
		ScmPayformEntity scmPayform = null;
		ScmPayformOrderEntity scmPayformOrder = null;
		scmPayform = iScmPayformService.getScmPayformById(pkPayFormId);
		String[] fkPurOrderArray = scmPayformParam.getFkPurOrders().split(",");

		if (null != scmPayform) {
			iScmPayformService.delScmPayformOrderByPayformId(pkPayFormId);// 根据
																			// 付款申请单Id，全部删除
																			// 付款申请单明细
			scmPayform.setModiTime(new Date());
			scmPayform.setApplyBy(userId);
			scmPayform.setModiBy(userId);
			if(!"2".equals(purStatus))
				scmPayform.setStatus("1");// 1 草稿状态
			scmPayform.setFkSupId(supplyId);
			scmPayform.setDelFlag("0");
			iScmPayformService.updateScmPayform(scmPayform);
		} else {
			scmPayform = new ScmPayformEntity();
			pkPayFormId = PrimaryKeyGenerator.getPrimaryKey(BusinessType.FK,
					user.getAbbrCode());
			String abbrCode = user.getBranId();
			scmPayform.setApplyBy(userId);
			scmPayform.setModiBy(userId);
			scmPayform.setFkBranId(abbrCode);
			scmPayform.setStatus("1");// 1 草稿状态
			scmPayform.setFkSupId(supplyId);
			scmPayform.setPkPayformId(pkPayFormId);
			scmPayform.setDelFlag("0");
			iScmPayformService.addScmPayform(scmPayform);
		}
		List<ScmPayformOrderEntity> scmPayFormOrderList = new ArrayList<ScmPayformOrderEntity>();
		for (int k = 0; k < fkPurOrderArray.length; k++) {
			scmPayformOrder = new ScmPayformOrderEntity();
			scmPayformOrder.setFkPayformId(pkPayFormId);
			scmPayformOrder.setFkPurordId(fkPurOrderArray[k]);
			scmPayFormOrderList.add(scmPayformOrder);
		}
		if (null != scmPayFormOrderList && scmPayFormOrderList.size() > 0) {
			iScmPayformService.addScmPayFormOrderBatch(scmPayFormOrderList);
		}
		scmPayform.setScmPayformOrderList(scmPayFormOrderList);
		if (saveType.equals("tijiao")) {
			if (StringUtils.isBlank(scmPayform.getFkFlowinstId())) {// 流程id为空时，设状态1
																	// ，
				purStatus = "1";// 1 草稿
			} else {
				purStatus = "2";// 2 审批中
			}
			if (purStatus.equals("1")) {// 门店提交订单审核
				String fkFlowId = workflowService.startWorkflow(ConstantUtil
						.getWorkflowKey(ConstantUtil.WorkflowKey.pay),
						pkPayFormId, user);
				scmPayform.setFkFlowinstId(fkFlowId);
				scmPayform.setStatus("2");
				scmPayform.setModiTime(new Date());
				iScmPayformService.updateScmPayform(scmPayform);
				//判断是否是门店
				if(StringUtil.isNotBlank(user.getBranId())){
					//如果是门店的话就进行数据上传
					if(user.getBranId().indexOf("ZB") == -1){
						ScmPayformEntity spay = iScmPayformService.getScmPayformById(scmPayform.getPkPayformId());
						List<ScmPayformOrderEntity> scmPayformOrderListOld = iScmPayformService.getPayFormOrderByFkPayformId(scmPayform.getPkPayformId());
						spay.setScmPayformOrderList(scmPayformOrderListOld);
						das.executePay(spay, "insert");
					}
				}
			}
			if (purStatus.equals("2")) {//退回 重新提交 审批
				WorkflowParamVo workflowparam = new WorkflowParamVo();
				workflowparam.setUserInfo(UserUtil.getUserFromSession());
				workflowparam.setHandleAdviceDes(ConstantUtil.WORKFLOW_STATUS_REAPPLY);
				workflowparam.setProcessInstanceId(scmPayform.getFkFlowinstId());
				workflowparam.setBusinessKey(pkPayFormId);
				//判断是否是门店
				if(StringUtil.isNotBlank(user.getBranId())){
					//如果是门店的话就进行数据上传
					if(user.getBranId().indexOf("ZB") == -1){
						ScmPayformEntity spay = iScmPayformService.getScmPayformById(scmPayform.getPkPayformId());
						List<ScmPayformOrderEntity> scmPayformOrderListOld = iScmPayformService.getPayFormOrderByFkPayformId(scmPayform.getPkPayformId());
						spay.setScmPayformOrderList(scmPayformOrderListOld);
						das.executePay(spay, "update");
					}
				}
				ProcessInstance processInstance = workflowService
						.complete(workflowparam);
				if (processInstance == null) {
//					scmPayform.setStatus(workflowparam.getHandleAdvice().equals("1") ? "3" : "4");
					scmPayform.setModiTime(new Date());
					iScmPayformService.updateScmPayform(scmPayform);
				}
			}
		}
		return "1";
	}

	/***
	 * 审批流程 提交
	 * 
	 * @param param
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/approvalProcessPayFormOrder")
	public ModelAndView approvalProcessPayFormOrder(WorkflowParamVo param)
			throws RDPException {
		ModelAndView model = new ModelAndView("invoicing/invo_orderPay");
		ScmPayformEntity entity = new ScmPayformEntity();
		entity = iScmPayformService.getScmPayformById(param.getBusinessKey());
		param.setUserInfo(UserUtil.getUserFromSession());
		ProcessInstance processInstance = workflowService.complete(param);
		if (processInstance == null) {
			entity.setModiTime(new Date());
			entity.setStatus(param.getHandleAdvice().equals("1") ? "3" : "4");
			iScmPayformService.updateScmPayform(entity);
		}
		return model;
	}

	/***
	 * 审批流程 退回
	 * 
	 * @param param
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月12日
	 */
	@RequestMapping("/approvalProcessPayFormOrderExt")
	public ModelAndView approvalProcessPayFormOrderExt(WorkflowParamVo param)
			throws RDPException {
		ModelAndView model = new ModelAndView("invoicing/invo_orderPay");

		ScmPayformEntity entity = new ScmPayformEntity();
		entity = iScmPayformService.getScmPayformById(param.getBusinessKey());
		param.setUserInfo(UserUtil.getUserFromSession());
		workflowService.backProcessToStart(param);
		entity.setStatus("2");
		entity.setModiTime(new Date());
		iScmPayformService.updateScmPayform(entity);
		return model;
	}

	/**
	 * 列表页面 点击 删除 按钮 ，付款申请单 状态 set 为 删除状态。
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/delPayFormById")
	@ResponseBody
	public String delPayFormById(HttpSession session,
			@RequestParam("pkPayFormId") String pkPayFormId)
			throws RDPException {
		ScmPayformEntity entity = new ScmPayformEntity();
		entity.setPkPayformId(pkPayFormId);
		entity.setStatus("0");
		entity.setDelFlag("1");
		entity.setModiTime(new Date());
		iScmPayformService.delScmPayformOrderByPayformId(pkPayFormId);
		iScmPayformService.updateScmPayform(entity);
		return "1";
	}

	/**
	 * 列表页面 点击 删除 按钮 ，付款申请单 状态 set 为 删除状态。
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/delScmPayformOrderByTwoId")
	@ResponseBody
	public String delScmPayformOrderByTwoId(
			@RequestParam("fkPayformId") String fkPayformId,
			@RequestParam("fkPurordId") String fkPurordId) throws RDPException {
		iScmPayformService.delScmPayformOrderByTwoId(fkPayformId, fkPurordId);
		return "1";
	}

	/**
	 * 返回时删除已经添加到数据库的采购订单
	 */
	@RequestMapping(value = "/delScmPayformOrder", method = RequestMethod.POST)
	@ResponseBody
	public String delScmPayformOrder(ScmPayformOrderEntity scmPayformOrder) {
		try {
			List<String> purordIdList = scmPayformOrder.getPurordIdList();
			if (purordIdList.size() > 0) {
				String fkPayformId = scmPayformOrder.getFkPayformId();
				for (String id : purordIdList) {
					iScmPayformService.delScmPayformOrderByTwoId(fkPayformId,
							id);
				}
			}
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
		return Constants.SUCCESS;
	}

	/**
	 * 付款申请单审批完成后，根据财务返回的票号，进行汇入保存
	 * 
	 * @Description String
	 * @author chenyu 2014年9月7日 下午9:47:32
	 */
	@RequestMapping("/savePiaoHao")
	@ResponseBody
	public String savePiaoHao(HttpSession session,
			@RequestParam("fkPayformId") String fkPayformId,
			@RequestParam("checkNo") String checkNo,
			@RequestParam("cableNo") String cableNo,
			@RequestParam("creditNo") String creditNo) throws RDPException {
		ScmPayformEntity entity = new ScmPayformEntity();
		entity.setPkPayformId(fkPayformId);
		if (StringUtils.isNotBlank(checkNo)) {
			entity.setCheckNo(checkNo);
		}
		if (StringUtils.isNotBlank(cableNo)) {
			entity.setCableNo(cableNo);
		}
		if (StringUtils.isNotBlank(creditNo)) {
			entity.setCreditNo(creditNo);
		}
		entity.setModiTime(new Date());
		iScmPayformService.updateScmPayform(entity);
		return "1";
	}

	/**
	 * 根据 采购订单Id 判断是否已经存在该采购订单
	 * 
	 * @param pkPayFormId
	 * @param fkPurordId
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "isAddPayformOrderAlready")
	@ResponseBody
	public String isAddPayformOrderAlready(String fkPurordId)
			throws RDPException {
		
		List<ScmPayformOrderEntity> scmPayformOrder = iScmPayformService.getPayFormOrderByPurordId(fkPurordId);
		if(null !=scmPayformOrder && scmPayformOrder.size()>0){
			ScmPayformOrderEntity payFormOrder =scmPayformOrder.get(0);
			String payFormId =payFormOrder.getFkPayformId();
			if(StringUtils.isNotBlank(payFormId)){
				ScmPayformEntity scmpayEntity=iScmPayformService.getScmPayformById(payFormId);
				if(null !=scmpayEntity){
					String delFlage =scmpayEntity.getDelFlag();
					if(delFlage.equals("1")){
						iScmPayformService.delScmPayformOrderByPayformId(payFormId);
						 scmPayformOrder = iScmPayformService.getPayFormOrderByPurordId(fkPurordId);
					}
				}
			}
		}
		if ( null ==scmPayformOrder|| scmPayformOrder.size() == 0) {
			return Constants.FAILURE;
		} else {
			return Constants.SUCCESS;
		}
	}
	
	/**
	 * 导出付款明细
	 * @throws RDPException
	 * @date: 2014年11月20日
	 */
	@RequestMapping(value="/exportPayDetailed")
	public void exportPayDetailed(ExportParamVo vo,HttpServletResponse response)throws RDPException{
		iScmPayformService.exportPayDetailed(vo, response);
	}
}
